package sdk.pay.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import java.util.Date;

import sdk.pay.basepay.UnifiedPay;
import sdk.pay.constant.PayConstant;
import sdk.pay.listener.PayUtilCallBack;
import sdk.pay.model.TimeDataModel;

/**
 * Created by Joe on 2017/6/26 0026 14:38.
 * timer mothods uitls
 */

public class PayTimeUtil {
    public static final String TIMER_TYPE_SERVICE = "Service";
    public static final String TIMER_TYPE_TOKEN = "Token";
    public static final String TIMER_TYPE_PAYLIST = "PayList";
    public static final String TIMER_TYPE_PAYURL = "PayUrl";
    public static final String TIMER_TYPE_CALLBACK = "CallBack";

    private long timerControl;
    private Activity mActivity;
    private TimeDataModel mTimeDataModel;
    private PayTimeStatisticsUtil mPayTimeStatisticsUtil;
    private PayUtilCallBack mPayUtilCallBack;
    private UnifiedPay.CallBack mCallBack;

    public PayTimeUtil(Activity activity) {
        mActivity = activity;
        mTimeDataModel = new TimeDataModel();
        mPayTimeStatisticsUtil = new PayTimeStatisticsUtil();
    }

    public PayTimeUtil(Activity activity, PayUtilCallBack payUtilCallBack) {
        mActivity = activity;
        mTimeDataModel = new TimeDataModel();
        mPayTimeStatisticsUtil = new PayTimeStatisticsUtil();
        mPayUtilCallBack = payUtilCallBack;
    }

    public PayTimeUtil(Activity activity, UnifiedPay.CallBack callBack) {
        mActivity = activity;
        mTimeDataModel = new TimeDataModel();
        mPayTimeStatisticsUtil = new PayTimeStatisticsUtil();
        mCallBack = callBack;
    }

    public void setTimeData(String msg, String type) {
        if (null == msg) {
            mTimeDataModel.initModelData();
            mTimeDataModel.setStartDate(new Date());
            return;
        } else {
            mTimeDataModel.setEndDate(new Date());
            mTimeDataModel.setExtraMessage(msg);
        }
        switch (type) {
            case TIMER_TYPE_SERVICE:
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setServiceTime(mTimeDataModel.getCostTime());
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setServiceInfo(mTimeDataModel.getExtraMessage());
                break;
            case TIMER_TYPE_TOKEN:
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setTokenTime(mTimeDataModel.getCostTime());
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setTokenInfo(mTimeDataModel.getExtraMessage());
                break;
            case TIMER_TYPE_PAYLIST:
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setPaylistTime(mTimeDataModel.getCostTime());
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setPaylistInfo(mTimeDataModel.getExtraMessage());
                break;
            case TIMER_TYPE_PAYURL:
                mPayTimeStatisticsUtil.getTimeStatisticsModel().initPayTimeData();
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setPayUrlTime(mTimeDataModel.getCostTime());
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setPayUrlInfo(mTimeDataModel.getExtraMessage());
                break;
            case TIMER_TYPE_CALLBACK:
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setCallbackTime(mTimeDataModel.getCostTime());
                mPayTimeStatisticsUtil.getTimeStatisticsModel().setCallbackInfo(mTimeDataModel.getExtraMessage());
                break;
            default:
                break;
        }
    }

    public void setTimeData(String msg) {
        if (null == msg) {
            mTimeDataModel.initModelData();
            mTimeDataModel.setStartDate(new Date());
            timerControl = PayConstant.PAY_TIME_OUT;
            new Thread(new timerRunnable()).start();
            return;
        } else {
            mTimeDataModel.setEndDate(new Date());
            mTimeDataModel.setExtraMessage(msg);
        }
        PayTimeStatisticsUtil utils = new PayTimeStatisticsUtil();
        utils.getTimeStatisticsModel().setPayTime(mTimeDataModel.getCostTime());
        utils.getTimeStatisticsModel().setPayInfo(mTimeDataModel.getExtraMessage());
        if (msg.startsWith("error:")) {
            utils.doErrorUpload();
            return;
        } else {
            timerControl = -2;
        }
        utils.insertData();
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    if (timerControl <= 0 && timerControl != -2) {
                        String errorMsg = "error: pay time out";
                        setTimeData(errorMsg);
                        dialogCancel(errorMsg);
                    } else if (timerControl > 0) {
                        timerControl--;
                    }
                    break;
            }
        }
    };

    private class timerRunnable implements Runnable {
        @Override
        public void run() {
            while (timerControl > 0 && timerControl != -2) {
                try {
                    Thread.sleep(1000);  // sleep 1000ms
                    mHandler.sendEmptyMessage(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    timerControl = -2;//pay timer error
                }
            }
        }
    }

    private void dialogCancel(final String msg) {
        if (mPayUtilCallBack != null) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mPayUtilCallBack.onPayException(msg);
                    PayToastUtil.showMessage(mActivity, msg);
                }
            });
        } else if (mCallBack != null) {
            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mCallBack.prepareLeave();
                    PayToastUtil.showMessage(mActivity, msg);
                }
            });
        }
    }

    public void setTimerControl(long timerValue) {
        timerControl = timerValue;
    }
}
